﻿#include "personinfoedit.h"
#include "ui_personinfoedit.h"
#include "opdb.h"
#include "login.h"
#include <QMessageBox>

PersonInfoEdit::PersonInfoEdit(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PersonInfoEdit)
{
    ui->setupUi(this);
    setWindowTitle(" ");
    setWindowFlags(Qt::FramelessWindowHint);//将窗口标题去掉
    //添加list控件内容
    ui->sex_comboBox->addItem("男");
    ui->sex_comboBox->addItem("女");
}

PersonInfoEdit::~PersonInfoEdit()
{
    delete ui;
}

void PersonInfoEdit::mousePressEvent(QMouseEvent *event)
{
    isPressedWidget = true; // 当前鼠标按下的是QWidget而非界面上布局的其它控件
    last = event->globalPos();
}

void PersonInfoEdit::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressedWidget)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        last = event->globalPos();
        move(x()+dx, y()+dy);
    }
}

void PersonInfoEdit::mouseReleaseEvent(QMouseEvent *event)
{
    int dx = event->globalX() - last.x();
    int dy = event->globalY() - last.y();
    move(x()+dx, y()+dy);
    isPressedWidget = false; // 鼠标松开时，置为false
}

QString PersonInfoEdit::getStartAndZodiac(QString time)
{
    QStringList qlist = time.split('-');
    QString str[]={"猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊"};
    QString sx = str[(qlist[0].toInt())%12];
    int num = qlist[1].toInt()*100+qlist[2].toInt();
    QString start;
    if(num>=321&&num<=419)
        start="白羊座";
    else if(num>=420&&num<=520)
        start="金牛座";
    else if(num>=521&&num<=621)
        start="双子座";
    else if(num>=622&&num<=722)
        start="巨蟹座";
    else if(num>=723&&num<=822)
        start="狮子座";
    else if(num>=823&&num<=922)
        start="处女座";
    else if(num>=923&&num<=1023)
        start="天平座";
    else if(num>=1024&&num<=1122)
        start="天蝎座";
    else if(num>=1123&&num<=1221)
        start="人马座";
    else if(num>=219&&num<=320)
        start="双鱼座";
    else if(num>=120&&num<=218)
        start="宝瓶座";
    else
        start="山羊座";
    return sx+"-"+start;
}

PersonInfoEdit &PersonInfoEdit::getInstance()
{
    static PersonInfoEdit personinfoedit;
    return personinfoedit;
}

void PersonInfoEdit::on_btn_close_clicked()
{
    close();
    destroy();
}

void PersonInfoEdit::on_btn_min_clicked()
{
    hide();
}

void PersonInfoEdit::on_btn_save_clicked()//保存更改
{
    QString age = ui->age->toPlainText();
    QString nickname = ui->nickname->toPlainText();
    QString sex = ui->sex_comboBox->currentText();
    QString phone = ui->lineEdit_phone->text();
    QString address = ui->lineEdit_address->text();
    QString time = ui->dateTimeEdit->dateTime().toString("yyyy-MM-dd");
    QString persig = ui->textEdit->toPlainText();
    QString rubric = ui->textEdit_2->toPlainText();
    QList<QString> list;
    list.append(Widget::getInstance().getName());
    list.append(age);list.append(nickname);list.append(sex);list.append(phone);
    list.append(address);list.append(time);list.append(persig);list.append(rubric);
    QString xas = getStartAndZodiac(time);
    QStringList qlist = xas.split('-');
    list.append(qlist[0]);list.append(qlist[1]);
    if(OpDb::getInstance().saveUserInfo(list))
    {
        QMessageBox::information(this,"提示","修改成功");
    }
}
